#include "qtwidgets1.h"
#include "ICalculator.h"
#include <QString>

QtWidgets1::QtWidgets1()
    : QMainWindow(NULL, Qt::WindowCloseButtonHint)
{
    
}              


bool QtWidgets1::construct()
{
  bool ret = true;

  ui.setupUi(this);
  connect(ui.pushButton_0       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_1       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_2       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_3       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_4       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_5       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_6       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_7       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_8       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_9       , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_plus    , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_left    , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_sub     , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_right   , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_multiply, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_del     , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_dot     , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_equ     , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_div     , SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  connect(ui.pushButton_clear   , SIGNAL(clicked()), this, SLOT(onButtonClicked()));

  return ret;
}

QtWidgets1* QtWidgets1::NewInstance()
{
  QtWidgets1* ret = new QtWidgets1();
  if (!(ret && ret->construct()))
  {
    delete ret;
    ret = NULL;
  }

  return ret;
}

void QtWidgets1::show()
{
  QWidget::show();
  setFixedSize(QSize(260, 250));
}

void QtWidgets1::setCalculator(ICalculator* cal)
{
  m_cal = cal;
}

ICalculator* QtWidgets1::getCalculator()
{
  return m_cal;
}

QtWidgets1::~QtWidgets1()
{

}

void QtWidgets1::onButtonClicked()
{
  QPushButton* btn = dynamic_cast<QPushButton*>(sender());
  if (btn != NULL)
  {
    QString clickText = btn->text();
    QString text = ui.lineEdit->text();
    if (clickText == "del")
    {
      if (text.length() > 0)
      {
        text.remove(text.length() - 1, 1);
        ui.lineEdit->setText(text);
      }
    }
    else if(clickText == "C")
    {
      ui.lineEdit->setText("");
    }
    else if (clickText == "=")
    {
      if (m_cal != NULL)
      {
        m_cal->expression(text);
        ui.lineEdit->setText(m_cal->result());
      }
    }
    else
    {
      ui.lineEdit->setText(text + clickText);
    }
  }
}
